<template>
  <view class="login-container">
    <view class="login-header">
      <view class="logo">
        <image src="@/static/logo.png" class="logo-image" />
      </view>
      <view class="title">美申智控系统</view>
      <view class="subtitle">便携式厕所物联网管理平台</view>
    </view>
    
    <view class="login-form">
      <view class="form-item">
        <input
          v-model="form.username"
          placeholder="请输入用户名"
          type="text"
          class="input"
          :disabled="loading"
        />
      </view>
      
      <view class="form-item">
        <input
          v-model="form.password"
          placeholder="请输入密码"
          type="password"
          class="input"
          :disabled="loading"
        />
      </view>
      
      <view class="form-actions">
        <button
          @click="handleLogin"
          :disabled="loading || !canSubmit"
          class="btn btn-primary btn-block"
          :class="{ 'btn-disabled': loading || !canSubmit }"
        >
          {{ loading ? '登录中...' : '登录' }}
        </button>
      </view>
    </view>
    
    <view class="login-footer">
      <view class="version">版本 1.0.0</view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { useUserStore } from '@/store/user'
import { useAppStore } from '@/store/app'
import config from '@/config'

const userStore = useUserStore()
const appStore = useAppStore()

// 表单数据
const form = ref({
  username: '',
  password: ''
})

const loading = ref(false)

// 是否可以提交
const canSubmit = computed(() => {
  return form.value.username.trim() !== '' && form.value.password.trim() !== ''
})

// 页面加载
onLoad(() => {
  // 如果已登录，跳转到首页
  if (userStore.isLoggedIn) {
    uni.reLaunch({
      url: config.pages.home
    })
  }
})

// 登录处理
const handleLogin = async () => {
  if (loading.value || !canSubmit.value) return
  
  try {
    loading.value = true
    
    await userStore.loginAction({
      username: form.value.username.trim(),
      password: form.value.password.trim()
    })
    
    appStore.showSuccess('登录成功')
    
    // 跳转到首页
    setTimeout(() => {
      uni.reLaunch({
        url: config.pages.home
      })
    }, 1000)
    
  } catch (error) {
    console.error('登录失败:', error)
  } finally {
    loading.value = false
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 60rpx 40rpx;
}

.login-header {
  text-align: center;
  margin-bottom: 80rpx;
  
  .logo {
    margin-bottom: 40rpx;
    
    .logo-image {
      width: 120rpx;
      height: 120rpx;
      border-radius: 24rpx;
    }
  }
  
  .title {
    font-size: 48rpx;
    font-weight: bold;
    color: #ffffff;
    margin-bottom: 16rpx;
  }
  
  .subtitle {
    font-size: 28rpx;
    color: rgba(255, 255, 255, 0.8);
  }
}

.login-form {
  width: 100%;
  max-width: 640rpx;
  background: #ffffff;
  border-radius: 24rpx;
  padding: 60rpx 40rpx;
  box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.1);
  
  .form-item {
    margin-bottom: 32rpx;
    
    .input {
      width: 100%;
      height: 88rpx;
      padding: 0 24rpx;
      border: 1rpx solid #e8e8e8;
      border-radius: 12rpx;
      font-size: 32rpx;
      background: #fafafa;
      
      &:focus {
        border-color: #007AFF;
        background: #ffffff;
      }
    }
  }
  
  .form-actions {
    margin-top: 40rpx;
    
    .btn {
      height: 88rpx;
      font-size: 32rpx;
      border-radius: 12rpx;
    }
  }
}

.login-footer {
  margin-top: 60rpx;
  text-align: center;
  
  .version {
    font-size: 24rpx;
    color: rgba(255, 255, 255, 0.6);
  }
}
</style>